﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _010_RemotingServerTCPActivated
{
    public class RemotingServer : MarshalByRefObject, _10_RemotingSharedAssembly.IServerInterface
    {
        private Storage _storage = new Storage();

        int _10_RemotingSharedAssembly.IServerInterface.AddRequest(_10_RemotingSharedAssembly.ServerRequest rq)
        {
            int index = _storage.Requests.Count;
            _storage.Requests.Add(index, rq);
            return index;
        }

        _10_RemotingSharedAssembly.ServerRequest _10_RemotingSharedAssembly.IServerInterface.GetRequest(int rq_id)
        {
            if (_storage.Requests.ContainsKey(rq_id))
            {
                return _storage.Requests[rq_id];
            }
            else throw new ArgumentException("Request not found");

        }

        void _10_RemotingSharedAssembly.IServerInterface.ProcessRequest(int rq_id)
        {
            Console.WriteLine("Processing ...");
            if (_storage.Requests.ContainsKey(rq_id))
            {
                _10_RemotingSharedAssembly.ServerRequest rq = _storage.Requests[rq_id];
                _10_RemotingSharedAssembly.ServerResponse resp = new _10_RemotingSharedAssembly.ServerResponse();

                resp.Array = new int [rq.Array.Length];
                for(int i=0;i<resp.Array.Length;i++) resp.Array[i] = rq.Array[rq.Array.Length-i-1];

                if (_storage.Response.ContainsKey(rq_id)) _storage.Response[rq_id] = resp;
                else                _storage.Response.Add(rq_id, resp);
            }
            else throw new ArgumentException("Request not found");
        }

        _10_RemotingSharedAssembly.ServerResponse _10_RemotingSharedAssembly.IServerInterface.GetResponse(int rq_id)
        {
            if (_storage.Response.ContainsKey(rq_id))
            {
                return _storage.Response[rq_id];
            }
            else throw new ArgumentException("Response not found");
        }
    }
}
